/**
 * 任务管理 API
 */
import type {
  HistoricTask,
  TaskCompleteForm,
  TaskQuery,
  UserTask,
} from './types';

import { requestClient } from '../request';

/**
 * 获取待办任务列表
 */
export async function getTodoTaskListApi(params: TaskQuery) {
  return requestClient.get<{
    list: UserTask[];
    total: number;
    pageNum: number;
    pageSize: number;
  }>('/workflow/api/task/todo', { params });
}

/**
 * 获取已办任务列表
 */
export async function getDoneTaskListApi(params: TaskQuery) {
  return requestClient.get<{
    list: HistoricTask[];
    total: number;
    pageNum: number;
    pageSize: number;
  }>('/workflow/api/task/done', { params });
}

/**
 * 获取任务详情
 */
export async function getTaskDetailApi(id: string) {
  return requestClient.get<UserTask>(`/workflow/api/task/${id}`);
}

/**
 * 完成任务
 */
export async function completeTaskApi(data: TaskCompleteForm) {
  return requestClient.post('/workflow/api/task/complete', data);
}

/**
 * 认领任务
 */
export async function claimTaskApi(taskId: string) {
  return requestClient.post(`/workflow/api/task/${taskId}/claim`);
}

/**
 * 取消认领任务
 */
export async function unclaimTaskApi(taskId: string) {
  return requestClient.post(`/workflow/api/task/${taskId}/unclaim`);
}

/**
 * 转办任务
 */
export async function delegateTaskApi(taskId: string, userId: string) {
  return requestClient.post(`/workflow/api/task/${taskId}/delegate`, {
    userId,
  });
}

/**
 * 退回任务
 */
export async function returnTaskApi(
  taskId: string,
  targetActivityId: string,
  comment?: string,
) {
  return requestClient.post(`/workflow/api/task/${taskId}/return`, {
    targetActivityId,
    comment,
  });
}

/**
 * 加签（前加签/后加签）
 */
export async function addSignTaskApi(
  taskId: string,
  userIds: string[],
  type: 'before' | 'after',
) {
  return requestClient.post(`/workflow/api/task/${taskId}/add-sign`, {
    userIds,
    type,
  });
}

/**
 * 获取任务的表单数据
 */
export async function getTaskFormDataApi(taskId: string) {
  return requestClient.get<Record<string, any>>(
    `/workflow/api/task/${taskId}/form-data`,
  );
}

/**
 * 获取任务的审批历史
 */
export async function getTaskCommentsApi(taskId: string) {
  return requestClient.get<
    Array<{
      id: string;
      userId: string;
      userName: string;
      message: string;
      time: string;
      type: string;
    }>
  >(`/workflow/api/task/${taskId}/comments`);
}

/**
 * 添加任务评论
 */
export async function addTaskCommentApi(taskId: string, message: string) {
  return requestClient.post(`/workflow/api/task/${taskId}/comment`, {
    message,
  });
}

